<?php
class DealOrderCode extends AppModel {

	var $name = 'DealOrderCode';
	
	var $belongsTo = array(
		'DealOrder' => array(
			'className' => 'Deals.DealOrder',
			'foreignKey' => 'deal_order_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
	);
	
	function markAsUsed($id)
	{
		$dealOrderCode = $this->find("first",array("conditions"=>array($this->alias.".id"=>$id),"recursive"=>-1));
		if(!$id || empty($dealOrderCode))
		{
			return false;
		}
		$return = false;
		if($this->updateAll(array($this->alias.'.is_used'=>1,$this->alias.'.used_on'=>date("Ymd")),array($this->alias.".id"=>$id)))
		{
			$return = true;
			if(!$this->hasAny(array($this->alias.".deal_order_id"=>$dealOrderCode[$this->alias]['deal_order_id'],$this->alias.".is_used"=>0)))
			{
				$this->DealOrder->updateAll(array('DealOrder.is_used'=>1,'DealOrder.used_on'=>date("Ymd")),array("DealOrder.id"=>$dealOrderCode[$this->alias]['deal_order_id']));
			}
		}
		return $return;
	}
	
	
}